<?	require_once '_lib/_ziplib.php';

if (!permitted('edit', $node = (int)param('node'))) {
	header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
	exit;
}

$DATA['components'] = array();

if (($temp = @glob($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.'*', GLOB_ONLYDIR)) && sizeof($temp)) {
	foreach ($temp as $t)
		$DATA['components'][] = substr(strrchr($t, "/"), 1);
}

if (($c = (int)param('export')) && ($temp = datafetch('db_item', tb('data'), "id_data=".D_MULTIBLOCK." AND id_parent in (".$DATA['page']['id'].", 0) AND id=$c")) && in_array($temp['value'], $DATA['components'])) {
	
	$DATA['component'] = array_merge($temp, array('path' => $_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.$temp['value']));
	
	if (file_exists("{$DATA[component][path]}/admin/export/init.php")) {

		$e = array();
		$e['document'] = new DOMDocument('1.0', 'utf-8');
		$e['root'] = $e['document']->createElement('sbse');
		$e['root']->setAttribute("type", $temp['value']);
		$e['document']->appendChild($e['root']);
		$e['attach'] = array();



		if (include("{$DATA[component][path]}/admin/export/init.php")) {	// end of component export
			header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'export')));
			exit;
		}
		

		$zipfile = new Ziplib;
		$zipfile->zl_add_file($e['document']->saveXML(), "data.xml", "g9");
		
		foreach($e['attach'] as $att) {
//			$attf = fopen($att['fName'], "rb");
//			$contents = fread($attf, filesize($att['fName']));
//			fclose($attf);
			
			$zipfile->zl_add_file(file_get_contents($att['fName']), $att['archName'], "g1");
		}
	
		header("Content-type: application/zip");
		header("Content-Disposition: attachment; filename=\"{$DATA[component][name]}.zip\"");
		echo $zipfile->zl_pack('');
	
		exit;
	}

} else {	// **** no such component ****

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'export')));
	exit;
}